#pragma once
#include <string>

#include <grpcpp/grpcpp.h>

#include "stack_chat_db.h"
#include "db_server.grpc.pb.h"
#include "grpc_db.h"

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;


grpc::Status DatabaseServiceImpl::UserSearchByID(grpc::ServerContext* context,
	const dbserver::SearchByIDRequest* request,
	dbserver::SearchByIDResponse* response) {
	int id = request->id();
	response->set_result(db.userSearchByID(id));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::UserGetNameByID(grpc::ServerContext* context,
	const dbserver::GetByIDRequest* request,
	dbserver::GetStringResponse* response) {
	int id = request->id();
	response->set_result(db.userGetNameByID(id));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::UserGetPasswordByID(grpc::ServerContext* context,
	const dbserver::GetByIDRequest* request,
	dbserver::GetStringResponse* response) {
	int id = request->id();
	response->set_result(db.userGetPasswordByID(id));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::UserUpdateNameByID(grpc::ServerContext* context,
	const dbserver::UpdateRequest* request,
	dbserver::UpdateResponse* response) {
	int id = request->id();
	std::string name = request->data();
	response->set_success(db.userUpdateNameByID(id, name));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::UserUpdatePasswordByID(grpc::ServerContext* context,
	const dbserver::UpdateRequest* request,
	dbserver::UpdateResponse* response) {
	int id = request->id();
	std::string password = request->data();
	response->set_success(db.userUpdatePasswordByID(id, password));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::UserDeleteByID(grpc::ServerContext* context,
	const dbserver::DeleteByIDRequest* request,
	dbserver::DeleteResponse* response) {
	int id = request->id();
	response->set_success(db.userDeleteByID(id));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::UserInsert(grpc::ServerContext* context,
	const dbserver::InsertRequest* request,
	dbserver::InsertResponse* response) {
	int id = request->id();
	std::string name = request->name(), password = request->password();
	response->set_success(db.userInsert(id, name, password));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::UserInfoSearchByID(grpc::ServerContext* context,
	const dbserver::SearchByIDRequest* request,
	dbserver::SearchByIDResponse* response) {
	int id = request->id();
	response->set_result(db.user_infoSearchByID(id));
	return grpc::Status::OK;
}


grpc::Status DatabaseServiceImpl::UserInfoInsert(grpc::ServerContext* context,
	const dbserver::UserInfoInsertRequest* request,
	dbserver::InsertResponse* response) {
	stack_chat_db::user_info uf;
	uf.age = request->age();
	uf.email = request->email();
	uf.gtime = request->gtime();
	uf.id = request->id();
	uf.info = request->info();
	uf.phone = request->phone();
	uf.sex = request->sex();
	response->set_success(db.user_infoInsert(uf));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::UserInfoDeleteByID(grpc::ServerContext* context,
	const dbserver::DeleteByIDRequest* request,
	dbserver::DeleteResponse* response) {
	int id = request->id();
	response->set_success(db.user_infoDeleteByID(id));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::UserInfoUpdateByID(grpc::ServerContext* context,
	const dbserver::UserInfoInsertRequest* request,
	dbserver::UpdateResponse* response) {
	stack_chat_db::user_info uf;
	uf.age = request->age();
	uf.email = request->email();
	uf.gtime = request->gtime();
	uf.id = request->id();
	uf.info = request->info();
	uf.phone = request->phone();
	uf.sex = request->sex();
	response->set_success(db.user_infoUpdateByID(uf));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::UserInfoGetInfoByID(grpc::ServerContext* context,
	const dbserver::GetByIDRequest* request,
	dbserver::UserInfoResponse* response) {
	int id = request->id();
	auto uf = db.user_infoGetInfoByID(id);
	response->set_age(uf.age);
	response->set_email(uf.email);
	response->set_phone(uf.phone);
	response->set_id(uf.id);
	response->set_gtime(uf.gtime);
	response->set_info(uf.info);
	response->set_sex(uf.sex);
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::ServerLogSearchByID(grpc::ServerContext* context,
	const dbserver::SearchByIDRequest* request,
	dbserver::SearchByIDResponse* response) {
	int id = request->id();
	response->set_result(db.server_logSearchByID(id));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::ServerLogInsert(grpc::ServerContext* context,
	const dbserver::ServerLogInsertRequest* request,
	dbserver::InsertResponse* response) {
	stack_chat_db::server_log sl;
	sl.data = request->data();
	sl.id = request->id();
	sl.time = request->time();
	sl.level = request->level();
	response->set_success(db.server_logInsert(sl));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::ServerLogDeleteByID(grpc::ServerContext* context,
	const dbserver::DeleteByIDRequest* request,
	dbserver::DeleteResponse* response) {
	int id = request->id();
	response->set_success(db.server_logDeleteByID(id));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::ServerLogUpdateByID(grpc::ServerContext* context,
	const dbserver::ServerLogInsertRequest* request,
	dbserver::UpdateResponse* response) {
	stack_chat_db::server_log sl;
	sl.data = request->data();
	sl.id = request->id();
	sl.time = request->time();
	sl.level = request->level();
	response->set_success(db.server_logUpdateByID(sl));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::ServerLogGetLogByID(grpc::ServerContext* context,
	const dbserver::GetByIDRequest* request,
	dbserver::ServerLogResponse* response) {
	int id = request->id();
	auto sl = db.server_logGetLogByID(id);
	response->set_id(sl.id);
	response->set_level(sl.level);
	response->set_data(sl.data);
	response->set_time(sl.time);
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::MessageLogSearchByID(grpc::ServerContext* context,
	const dbserver::SearchByIDRequest* request,
	dbserver::SearchByIDResponse* response) {
	int id = request->id();
	response->set_result(db.message_logSearchByID(id));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::MessageLogGetMessageByUserID(grpc::ServerContext* context,
	const dbserver::GetByIDRequest* request,
	dbserver::GetStringResponse* response) {
	int id = request->id();
	response->set_result(db.message_logGetMessageByUserID(id));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::MessageLogInsert(grpc::ServerContext* context,
	const dbserver::MessageLogInsertRequest* request,
	dbserver::InsertResponse* response) {
	stack_chat_db::message_log ml;
	ml.id = request->id();
	ml.time = request->time();
	ml.receive_id = request->receive_id();
	ml.send_id = request->send_id();
	ml.status = request->status();
	ml.data = request->data();
	response->set_success(db.message_logInsert(ml));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::FriendTableSearchByID(grpc::ServerContext* context,
	const dbserver::SearchByIDRequest* request,
	dbserver::SearchByIDResponse* response) {
	int id = request->id();
	response->set_result(db.friend_tableSearchByID(id));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::FriendTableGetFriendsByUserID(grpc::ServerContext* context,
	const dbserver::GetByIDRequest* request,
	dbserver::GetStringResponse* response) {
	int id = request->id();
	response->set_result(db.friend_tableGetFriendsByUserID(id));
	return grpc::Status::OK;
}

grpc::Status DatabaseServiceImpl::FriendTableIsFriend(grpc::ServerContext* context,
	const dbserver::IsFriendRequest* request,
	dbserver::IsFriendResponse* response) {
	int ida = request->ida(), idb = request->idb();
	response->set_is_friend(db.friend_tableIsFriend(ida, idb));
	return grpc::Status::OK;
}

grpc::Status  DatabaseServiceImpl::FriendTableDeleteByUserID(grpc::ServerContext* context,
	const dbserver::DeleteByUserIDRequest* request,
	dbserver::DeleteResponse* response) {
	int ida = request->ida(), idb = request->idb();
	response->set_success(db.friend_tableDeleteByUserID(ida, idb));
	return grpc::Status::OK;
}

grpc::Status  DatabaseServiceImpl::FriendTableInsert(grpc::ServerContext* context,
	const dbserver::FriendTableInsertRequest* request,
	dbserver::InsertResponse* response) {
	stack_chat_db::friend_table ft;
	ft.friend_id = request->friend_id();
	ft.friend_time = request->friend_time();
	ft.user_id_a = request->user_id_a();
	ft.user_id_b = request->user_id_b();

	response->set_success(db.friend_tableInsert(ft));
	return grpc::Status::OK;
}